﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace QnD_windowscron
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer.Tick += new EventHandler(timer_Tick);
            
        }

        Process p = new Process();
        bool isrunning = false;
        void timer_Tick(object sender, EventArgs e)
        {
            System.Threading.Thread runner = new System.Threading.Thread(new System.Threading.ThreadStart(runnervoid));
            runner.Start();
            
        }

        Timer timer = new Timer();
        bool running = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if (!running)
            {



                if (System.IO.File.Exists(textBox1.Text))
                {

                    switch (comboBox1.Text)
                    {
                        case "Idle":
                            //p.PriorityClass = ProcessPriorityClass.Idle;
                            break;
                        case "Normal":
                            //p.PriorityClass = ProcessPriorityClass.Normal;
                            break;
                        case "Below normal":
                            //p.PriorityClass = ProcessPriorityClass.BelowNormal;
                            break;
                        case "Above normal":
                            //p.PriorityClass = ProcessPriorityClass.AboveNormal;
                            break;
                        case "High":
                            //p.PriorityClass = ProcessPriorityClass.High;
                            break;
                        case "Realtime(!!)":
                            //p.PriorityClass = ProcessPriorityClass.RealTime;
                            break;

                        default:
                            MessageBox.Show("Invalid priority chosen, defaulting to 'Below Normal'.");
                            break;
                            
                    }
                    p.StartInfo.FileName = textBox1.Text;
                    timer.Interval = Convert.ToInt32(numericUpDown1.Value) * 60000;
                    button1.Text = "Stop";
                    
                    
                    CheckForIllegalCrossThreadCalls = false;
                    timer.Start();
                    button2.Enabled = false;
                    textBox1.Enabled = false;
                    numericUpDown1.Enabled = false;
                    comboBox1.Enabled = false;
                    running = true;

                }
                else
                {
                    MessageBox.Show("Set a file to use");
                }
            }
            else
            {
                button1.Text = "Start";
                timer.Stop();
                button2.Enabled = true;
                textBox1.Enabled = true;
                numericUpDown1.Enabled = true;
                comboBox1.Enabled = true;
                running = false;
            }
        }

        public void runnervoid()
        {
            if (!checkBox_allowmultipleinstances.Checked && isrunning)
                return;

            isrunning = true;
            try
            {
                listBox1.Items.Add(DateTime.Now.ToString() + " Running: " + textBox1.Text);
                DateTime dtbefore = DateTime.Now;

                p.Start();
                switch (comboBox1.Text)
                {
                    case "Idle":
                        p.PriorityClass = ProcessPriorityClass.Idle;
                        break;
                    case "Normal":
                        p.PriorityClass = ProcessPriorityClass.Normal;
                        break;
                    case "Below normal":
                        p.PriorityClass = ProcessPriorityClass.BelowNormal;
                        break;
                    case "Above normal":
                        p.PriorityClass = ProcessPriorityClass.AboveNormal;
                        break;
                    case "High":
                        p.PriorityClass = ProcessPriorityClass.High;
                        break;
                    case "Realtime(!!)":
                        p.PriorityClass = ProcessPriorityClass.RealTime;
                        break;

                    default:
                        listBox1.Items.Add("Invalid priority chosen, defaulting to 'Below normal'.");
                        p.PriorityClass = ProcessPriorityClass.BelowNormal;
                        break;

                }
                p.WaitForExit();

                
                listBox1.Items.Add("It took: " + (DateTime.Now - dtbefore).TotalSeconds + " Seconds.");
                isrunning = false;
            }
            catch (Exception ex)
            {
                listBox1.Items.Add(DateTime.Now.ToString() + " FAILED: " + textBox1.Text + "Reason: " + ex.Message);
                isrunning = false;

            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
            } 
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
